/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.loja.view.component.aggregation;

import br.com.loja.model.service.Service;
import br.com.loja.model.service.exception.ServiceException;
import br.com.loja.model.service.factory.ServiceFactory;
import br.com.loja.view.component.factory.ViewHadler;
import br.com.loja.view.swing.model.EntityComboBoxModel;
import javax.swing.JComboBox;

/**
 *
 * @author DavidLuiz
 */
public class EntityToOne<T> extends JComboBox<T> implements ViewHadler<T> {

    private final EntityComboBoxModel<T> comboBoxModel;

    public EntityToOne(Class<T> clazz) throws ServiceException {
        ServiceFactory serviceFactory = ServiceFactory.getServiceFactory();
        Service<T> service = serviceFactory.getService(clazz);
        comboBoxModel = new EntityComboBoxModel<>(service.list());
        setModel(comboBoxModel);

    }

    @Override
    public void setValueModel(T value) {
        getModel().setSelectedItem(value);
        updateUI();
    }

    @Override
    public T getValueModel() {
        return (T) getModel().getSelectedItem();
    }

}
